package org.apache.harmony.javax.security.auth;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Principal;
import java.util.Iterator;
import java.util.Set;

public final class PrivateCredentialPermission extends Permission
{
  private static final String READ = "read";
  private static final long serialVersionUID = 2068873036L;
  private String credentialClass;
  private transient int offset;
  private transient CredOwner[] set;

  public PrivateCredentialPermission(String paramString1, String paramString2)
  {
    super(paramString1);
    if ("read".equalsIgnoreCase(paramString2))
    {
      initTargetName(paramString1);
      return;
    }
    throw new IllegalArgumentException("auth.11");
  }

  PrivateCredentialPermission(String paramString, Set<Principal> paramSet)
  {
    super(paramString);
    this.credentialClass = paramString;
    this.set = new CredOwner[paramSet.size()];
    Iterator localIterator = paramSet.iterator();
    if (!(localIterator.hasNext()))
      return;
    Principal localPrincipal = (Principal)localIterator.next();
    CredOwner localCredOwner = new CredOwner(localPrincipal.getClass().getName(), localPrincipal.getName());
    int i = 0;
    int j = 0;
    while (true)
    {
      if (j >= this.offset);
      while (true)
      {
        while (true)
        {
          if (i != 0);
          CredOwner[] arrayOfCredOwner = this.set;
          int k = this.offset;
          this.offset = (k + 1);
          arrayOfCredOwner[k] = localCredOwner;
        }
        if (!(this.set[j].equals(localCredOwner)))
          break;
        i = 1;
      }
      ++j;
    }
  }

  private void initTargetName(String paramString)
  {
    if (paramString == null)
      throw new NullPointerException("auth.0E");
    String str = paramString.trim();
    if (str.length() == 0)
      throw new IllegalArgumentException("auth.0F");
    int i = str.indexOf(32);
    if (i == -1)
      throw new IllegalArgumentException("auth.10");
    this.credentialClass = str.substring(0, i);
    int j = i + 1;
    int k = 0;
    int l = str.length();
    while (true)
    {
      if (j >= l)
      {
        if (k >= 1)
          break;
        throw new IllegalArgumentException("auth.10");
      }
      int i1 = str.indexOf(32, j);
      int i2 = str.indexOf(34, i1 + 2);
      if ((i1 == -1) || (i2 == -1) || (str.charAt(i1 + 1) != '"'))
        throw new IllegalArgumentException("auth.10");
      j = i2 + 2;
      ++k;
    }
    int i3 = 1 + str.indexOf(32);
    this.set = new CredOwner[k];
    int i4 = 0;
    if (i4 >= k)
      return;
    int i5 = str.indexOf(32, i3);
    int i6 = str.indexOf(34, i5 + 2);
    CredOwner localCredOwner = new CredOwner(str.substring(i3, i5), str.substring(i5 + 2, i6));
    int i7 = 0;
    int i8 = 0;
    while (true)
    {
      if (i8 >= this.offset);
      while (true)
      {
        while (true)
        {
          if (i7 == 0)
          {
            CredOwner[] arrayOfCredOwner = this.set;
            int i9 = this.offset;
            this.offset = (i9 + 1);
            arrayOfCredOwner[i9] = localCredOwner;
          }
          i3 = i6 + 2;
          ++i4;
        }
        if (!(this.set[i8].equals(localCredOwner)))
          break;
        i7 = 1;
      }
      ++i8;
    }
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws IOException, ClassNotFoundException
  {
    paramObjectInputStream.defaultReadObject();
    initTargetName(getName());
  }

  private boolean sameMembers(Object[] paramArrayOfObject1, Object[] paramArrayOfObject2, int paramInt)
  {
    int i;
    if ((paramArrayOfObject1 == null) && (paramArrayOfObject2 == null))
      i = 1;
    while (true)
    {
      return i;
      if ((paramArrayOfObject1 != null) && (paramArrayOfObject2 != null))
        break;
      i = 0;
    }
    int j = 0;
    while (true)
    {
      while (j >= paramInt)
        i = 1;
      int k = 0;
      int l = 0;
      while (true)
      {
        if (l >= paramInt);
        while (true)
        {
          while (true)
          {
            if (k != 0)
              break label92;
            i = 0;
          }
          if (!(paramArrayOfObject1[j].equals(paramArrayOfObject2[l])))
            break;
          k = 1;
        }
        ++l;
      }
      label92: ++j;
    }
  }

  public boolean equals(Object paramObject)
  {
    int i;
    if (paramObject == this)
      i = 1;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return i;
          if ((paramObject != null) && (getClass() == paramObject.getClass()))
            break;
          i = 0;
        }
        PrivateCredentialPermission localPrivateCredentialPermission = (PrivateCredentialPermission)paramObject;
        if ((!(this.credentialClass.equals(localPrivateCredentialPermission.credentialClass))) || (this.offset != localPrivateCredentialPermission.offset) || (!(sameMembers(this.set, localPrivateCredentialPermission.set, this.offset))))
          break;
        i = 1;
      }
      i = 0;
    }
  }

  public String getActions()
  {
    return "read";
  }

  public String getCredentialClass()
  {
    return this.credentialClass;
  }

  public String[][] getPrincipals()
  {
    int i = this.offset;
    int[] arrayOfInt = new int[2];
    arrayOfInt[0] = i;
    arrayOfInt[1] = 2;
    String[][] arrayOfString = (String[][])Array.newInstance(String.class, arrayOfInt);
    int j = 0;
    while (true)
    {
      if (j >= arrayOfString.length)
        return arrayOfString;
      arrayOfString[j][0] = this.set[j].principalClass;
      arrayOfString[j][1] = this.set[j].principalName;
      ++j;
    }
  }

  public int hashCode()
  {
    int i = 0;
    int j = 0;
    while (true)
    {
      if (j >= this.offset)
        return (i + getCredentialClass().hashCode());
      i += this.set[j].hashCode();
      ++j;
    }
  }

  public boolean implies(Permission paramPermission)
  {
    int i;
    PrivateCredentialPermission localPrivateCredentialPermission;
    if ((paramPermission == null) || (getClass() != paramPermission.getClass()))
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        localPrivateCredentialPermission = (PrivateCredentialPermission)paramPermission;
        if (("*".equals(this.credentialClass)) || (this.credentialClass.equals(localPrivateCredentialPermission.getCredentialClass())))
          break;
        i = 0;
      }
      if (localPrivateCredentialPermission.offset != 0)
        break;
      i = 1;
    }
    CredOwner[] arrayOfCredOwner1 = this.set;
    CredOwner[] arrayOfCredOwner2 = localPrivateCredentialPermission.set;
    int j = this.offset;
    int k = localPrivateCredentialPermission.offset;
    int l = 0;
    while (true)
    {
      while (l >= j)
        i = 1;
      int i1 = 0;
      while (true)
      {
        if (i1 >= k);
        do
          while (true)
          {
            if (i1 != arrayOfCredOwner2.length)
              break label151;
            i = 0;
          }
        while (arrayOfCredOwner1[l].implies(arrayOfCredOwner2[i1]));
        ++i1;
      }
      label151: ++l;
    }
  }

  public PermissionCollection newPermissionCollection()
  {
    return null;
  }

  private static final class CredOwner
  implements Serializable
  {
    private static final long serialVersionUID = -1190955978L;
    private transient boolean isClassWildcard;
    private transient boolean isPNameWildcard;
    String principalClass;
    String principalName;

    CredOwner(String paramString1, String paramString2)
    {
      if ("*".equals(paramString1))
        this.isClassWildcard = true;
      if ("*".equals(paramString2))
        this.isPNameWildcard = true;
      if ((this.isClassWildcard) && (!(this.isPNameWildcard)))
        throw new IllegalArgumentException("auth.12");
      this.principalClass = paramString1;
      this.principalName = paramString2;
    }

    public boolean equals(Object paramObject)
    {
      int i;
      if (paramObject == this)
        i = 1;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return i;
            if (!(paramObject instanceof CredOwner))
              break label59;
            CredOwner localCredOwner = (CredOwner)paramObject;
            if ((!(this.principalClass.equals(localCredOwner.principalClass))) || (!(this.principalName.equals(localCredOwner.principalName))))
              break;
            i = 1;
          }
          i = 0;
        }
        label59: i = 0;
      }
    }

    public int hashCode()
    {
      return (this.principalClass.hashCode() + this.principalName.hashCode());
    }

    boolean implies(Object paramObject)
    {
      int i;
      if (paramObject == this)
        i = 1;
      while (true)
      {
        while (true)
        {
          return i;
          CredOwner localCredOwner = (CredOwner)paramObject;
          if (((!(this.isClassWildcard)) && (!(this.principalClass.equals(localCredOwner.principalClass)))) || ((!(this.isPNameWildcard)) && (!(this.principalName.equals(localCredOwner.principalName)))))
            break;
          i = 1;
        }
        i = 0;
      }
    }
  }
}